<!--
 * @Date: 2022-03-26 09:53:00
 * @LastEditors: sailornpg
 * @LastEditTime: 2022-11-19 13:19:23
 * @FilePath: \vue-admin-ts\src\components\menu\Mymenu.vue
 * @Description: something
-->
<template>
  <el-menu
    class="el-menu-vertical-demo"
    :default-active="defaultActive"
    :router="router"
    v-bind="$attrs"
  >
    <template v-for="(item, i) in data" :key="i">
      <el-menu-item
        v-if="!item.children || !item.children.length"
        :index="item.index"
      >
        <component
          v-if="item.icon"
          :is="`el-icon-${toLine(item.icon)}`"
          style="width: 1em; height: 1em; margin-right: 5px"
        ></component>
        <span>{{ item.name }}</span>
      </el-menu-item>
      <el-sub-menu
        v-if="item.children && item.children.length"
        :index="item.index"
      >
        <template #title>
          <component
            v-if="item.icon"
            :is="`el-icon-${toLine(item.icon)}`"
            style="width: 1em; height: 1em; margin-right: 5px"
          ></component>
          <span>{{ item.name }}</span>
        </template>
        <el-menu-item
          v-for="(item1, index1) in item.children"
          :key="index1"
          :index="item1.index"
          @click="clickHandler(item1)"
        >
          <component
            v-if="item1.icon"
            :is="`el-icon-${toLine(item1.icon)}`"
            style="width: 1em; height: 1em; margin-right: 5px"
          ></component>
          <span>{{ item1.name }}</span>
        </el-menu-item>
      </el-sub-menu>
    </template>
  </el-menu>
</template>
<script lang="ts" setup>
import { MenuItem } from "./type";
import { PropType } from "vue";
const toLine = (value: string) => {
  return value.replace(/(A-Z)g/, "-$1").toLocaleLowerCase();
};
const emits = defineEmits(["clickItem"]);
const props = defineProps({
  data: {
    type: Array as PropType<MenuItem[]>,
    required: true,
  },
  defaultActive: {
    type: String,
    default: "",
  },
  router: {
    type: Boolean,
    default: false,
  },
});
const clickHandler = (item: any) => {
  emits("clickItem", item);
  // console.log(item);
};
</script>
<style lang="less" scoped></style>
